﻿using AutoMapper;
using LianChuDemo.Bll.BaseInfoManagerBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WenDangOne.DbModel.BaseInfoManager;
using WenDangOne.ViewModel.BaseInfoManagerView;
using WenDangOne.ViewModel.common;

namespace WenDangOne.Controllers.BaseInfoManager
{
    public class WebSystemMenuManagerController : MyBaseController
    {
        private WebSystemMenuBll _webSystemMenuBll;
        private IMapper _mapper;
        public WebSystemMenuManagerController(WebSystemMenuBll webSystemMenuBll, IMapper mapper)
        {
            _webSystemMenuBll = webSystemMenuBll;
            _mapper = mapper;
        }

        //获取数据
        [HttpGet]
        public ResultModel<List<WebSystemMenu>> GetModuleDbsAll()
        {
            try
            {
                var data = _webSystemMenuBll.GetWebSystemMenuAll();
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<WebSystemMenu>>(ex);
            }
        }
        //查询view
        [HttpGet]
        public ResultModel<List<WebSystemMenuView>> GetWebSystemMenuViewsAll()
        {
            try
            {
                var data = _webSystemMenuBll.GetWebSystemMenuViewsAll();
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<WebSystemMenuView>>(ex);
            }
        }
        [HttpGet]
        public ResultModel<bool> VMenuCode(string wmc)
        {
            //接口验证
            try
            {
                var data = _webSystemMenuBll.VMenuCode(wmc);

                return MyOk<bool>(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        [HttpPost]
        public ResultModel<bool> WSMAdd(WebSystemMenu wsm)
        {
            if (string.IsNullOrWhiteSpace(wsm.wm_code))
            {
                return MyFail<bool>("请输入菜单code");
            }
            if (!_webSystemMenuBll.VMenuCode(wsm.wm_code))
            {
                return MyFail<bool>("菜单code已经被使用,请重新输入其他菜单Code");
            }


            wsm.creator_date = DateTime.Now;
            wsm.creator_name = LoginUser.user_name;
            try
            {
                var data = _webSystemMenuBll.MenuAdd(wsm);
                return MyOk<bool>(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="wsm"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel<bool> WebSystemMenuUpdate(WebSystemMenu wsm)
        {
            if (string.IsNullOrWhiteSpace(wsm.wm_code))
            {
                return MyFail<bool>("参数错误, 缺少module_code参数");
            }
            WebSystemMenu webSystem = _mapper.Map<WebSystemMenu>(wsm);
            webSystem.modifi_date = DateTime.Now;
            webSystem.modifi_name = LoginUser.user_name;
            try
            {
                var data = _webSystemMenuBll.WebSystemMenuUpdate(webSystem);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        /// <summary>
        /// 获取父菜单的名称  GetWebSystemMenuViewByCode
        /// </summary>
        /// <param name="wMCode"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel<WebSystemMenuView> GetMViewByCode(string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                return MyFail<WebSystemMenuView>("参数错误, 缺少code参数");
            }
            try
            {
                var data = _webSystemMenuBll.GetWebSystemMenuViewByCode(code);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<WebSystemMenuView>(ex);
            }

        }
        /// <summary>
        /// 根据用户获取层级网页系统菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultModel<List<WebSystemMenuView>> GetUserWebSystemMenu()
        {
            try
            {
                var data = _webSystemMenuBll.GetUserWebSystemMenu();
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<WebSystemMenuView>>(ex);
            }
        }

    }
}
